package com.bjim.model;

import java.util.List;

import com.bjim.common.BaseModel;

import cn.dreampie.orm.Record;
import cn.dreampie.orm.annotation.Table;

/**
 * 群组表映射类
 * @author chen
 *
 */
@Table(name = "ts_group",primaryKey="groupid",generatedKey="groupid")
public class Group extends BaseModel<Group>{
	public static Group dao=new Group();
	
	/**
	 * 根据群组名查找群组
	 * @param groupname
	 * @return
	 */
	public Group findByGroupname(String groupname){
		return this.dao.findFirstBy("groupname=?", groupname);
	}
	
	/**
	 * 根据群组ID和userid查找群组信息
	 * @param groupid
	 * @param userid
	 * @return
	 */
	public Group findGroup(int groupid,Object userid){
		Group group=Group.dao.findFirst("select a.*,b.isnotify,b.isadmin,b.istop from ts_group a,ts_usergroup b where a.groupid=? and a.groupid=b.groupid and b.userid=?  ",groupid,userid);
		return group;
	}
	
	/**
	 * 根据群组ID查找群组人员列表
	 * @return
	 */
	public String groupUsers(){
		String users="";
		List<UserGroup> usersgroups=UserGroup.dao.findBy("groupid=?", this.get("groupid").toString());
		for(int i=0;i<usersgroups.size();i++){
			UserGroup ug=usersgroups.get(i);
			users+=ug.get("userid");
			users+=",";
		}
		System.out.println(users);
		return users;
	}
	
}
